package manage.action;

import m.common.action.ActionMeta;
import m.common.action.VuePageResult;
import m.common.action.VueResult;
import m.common.model.type.FieldType;
import m.common.model.util.ModelQueryList;
import m.system.document.DocumentMeta;
import m.system.document.DocumentMethodMeta;
import m.system.document.DocumentParamMeta;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;
import manage.model.DictionaryType;
import manage.service.DictionaryTypeService;
import manage.util.DictionaryUtil;

@ActionMeta(name="manage/dictionaryType",title="系统-数据字典")
public class DictionaryTypeAction extends ManageAction {
	
	private DictionaryType model;

	@DocumentMeta(
		method=@DocumentMethodMeta(title="获取数据字典",result = "{code:#{code},map:{\"值\":\"内容|状态|parentOid\"}}"),
		params={
			@DocumentParamMeta(name="model.type",description="数据字典类型",type=FieldType.STRING,length=20,notnull=true,dictType = "manage.model.DictionaryType")
		}
	)
	public JSONMessage getDict(){
		JSONMessage result=new JSONMessage();
		try {
			result.push("map", DictionaryUtil.get(model.getType()));
			fillJSONResult(result,true,"");
		} catch (Exception e) {
			fillJSONResult(result,false,e);
		}
		return result;
	}
	public JSONMessage getList(){
		JSONMessage result=new JSONMessage();
		try {
			result.push("list", ModelQueryList.getModelList(DictionaryType.class,"*"));
			fillJSONResult(result,true,"");
		} catch (Exception e) {
			fillJSONResult(result,false,e);
		}
		return result;
	}
	public JSONMessage doSave(){
		JSONMessage result=new JSONMessage();
		try {
			verifyAdminOperPower("manage_system_power");
			getService(DictionaryTypeService.class).save(model);
			result.push("oid",model.getOid());
			fillJSONResult(result,true,"保存成功");
		} catch (Exception e) {
			fillJSONResult(result,false,e);
		}
		return result;
	}
	public VueResult toEdit() throws Exception {
		verifyAdminOperPower("manage_system_power");
		if(null!=model&&!StringUtil.isSpace(model.getOid())) {
			model=ModelQueryList.getModel(model,"*");
		}else if(null==model){
			model=new DictionaryType();
		}
		return new VuePageResult("page/base/dict/editType.js")
			.addParam("model",model);
	}
	public VueResult toList() throws Exception {
		verifyMenu("A1105");
		return new VuePageResult("page/base/dict/typeList.js");
	}
	public DictionaryType getModel() {
		return model;
	}
	public void setModel(DictionaryType model) {
		this.model = model;
	}

}
